/**
 * 
 */
package de.wesemann.mediahandbook.le.adapter;

import java.util.List;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.wesemann.jdedun.entities.district.districts.DistrictDistrict;
import de.wesemann.mediahandbook.le.R;

/**
 * @author Lars W.
 * 
 */
public class DistrictAdapter extends ArrayAdapter<DistrictDistrict> {
	private static final String LOG_TAG = DistrictAdapter.class.getSimpleName();
	private List<DistrictDistrict> items;
	private final Activity context;

	public DistrictAdapter(Activity context, List<DistrictDistrict> items) {
		super(context, R.layout.district_row, items);
		this.context = context;
		this.items = items;

	}

	static class ViewHolder {
		public TextView districtName;
//		public TextView districtPostcode;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			LayoutInflater vi = context.getLayoutInflater();
			convertView = vi.inflate(R.layout.district_row, null, true);

			holder = new ViewHolder();

			holder.districtName = (TextView) convertView.findViewById(R.id.tv_district_name);
//			holder.districtPostcode = (TextView) convertView.findViewById(R.id.tv_district_postcode);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (items.get(position).getName() != null) {
			holder.districtName.setText(items.get(position).getName());
		} else {
			Log.d(LOG_TAG, "no name found for id " + items.get(position).getId());
		}
		return convertView;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public DistrictDistrict getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return items.get(position).hashCode();
	}

	public void setItems(List<DistrictDistrict> districts) {
		notifyDataSetInvalidated();
		this.items = districts;
		notifyDataSetChanged();
	}

	public void addItems(List<DistrictDistrict> districts) {
		if (districts != null && !(districts.isEmpty())) {
			// this.items.addAll(companies);
			for (DistrictDistrict dist : districts) {
				add(dist);
			}
			notifyDataSetChanged();
		}

	}
}
